<?php 
      
       
      class ajax extends model  { 
           private $db;
           private $info;
           private $post;
           private $get;
           private $arraydata=array();
      
           function __construct($db,$info)
           { 
             global $memcache;
             $this->db=$db;   
             $this->info=$info;
             $this->memcache=$memcache;
             $this->db->get_connect();
             
             
             $this->setdb($this->db);
             
            
             if($_POST)
             {
             $this->post=$_POST;
             }
             if($_GET)
             {
             $this->get=$_GET;
             } 
             
             if($this->get['nojson']!=1)
             {
             header('Content-type: application/json');
             }
             
             
             if(method_exists($this,$this->info['parameter'][0]))
             {
              eval("\$this->".$this->info['parameter'][0]."();");     
                 
             }else
             {
                $this->arraydata['error']="No Medthod This Value";
                
             }
                 
                                    
           
           }
           
           function  savemembershelf()
           {
               
             if($_COOKIE['oauth'])
             {
             
             
             
             
             
             
             
             
             }else
             {
             
               $this->arraydata['error']="ยังไม่ได้ login"; 
             
             }   
          
                
                
             
           }
           
           function test()
           {
             
             //  $password1=md5(baseinfo.'-'.'robot4464');
//               echo  $password1; 

$d = new DateTime( '2039-02-23' ); 
echo $d->format( 't' );
             
            }
             
           function login()
           {
          
              if($this->post['username']&&$this->post['password1'])
              {
               $password1=md5(baseinfo.'-'.$this->post['password1']);
               $password2=md5('http://meetme.thaihealth.or.th'.'-'.$this->post['password1']);
               $password3=md5('http://newmeetme.dmconnex.com'.'-'.$this->post['password1']);
               $sql="select userid,`status`,username,email,`unlimited`,oauth,shelfid,photo from tb_user where (tb_user.username='".$this->post['username']."' or tb_user.email='".$this->post['username']."'  )  and  tb_user.`status` != 0 and (tb_user.password1='".$password1."' or tb_user.password1='".$password2."' or tb_user.password1='".$password3."' ) ";
               
               //echo $sql;
                
               $arrayuser=$this->db->db_set_recordset($sql);  
               if($arrayuser[0]['userid']&&$arrayuser[0]['oauth']&&$arrayuser[0]['status'])
               {
                if($this->post['rememberme'])
                {
                  setcookie("oauth",$arrayuser[0]['oauth'], time()+3600000, "/");
                }else
                {
                  setcookie("oauth",$arrayuser[0]['oauth'],0,'/'); 
                }
                
                  $this->arraydata['oauth']=$arrayuser[0]['oauth'];
                  $this->arraydata['username']=$arrayuser[0]['username'];
                  $this->arraydata['email']=$arrayuser[0]['email'];
                  $this->arraydata['unlimited']=$arrayuser[0]['unlimited'];  
                  $this->arraydata['avatar']="";  
                if($arrayuser[0]['photo'])  
                {
                $this->arraydata['avatar']=imginfo.'/user/'.$arrayuser[0]['userid'].'/'.$arrayuser[0]['photo'];
                }
                
                
                if($arrayuser[0]['status']==2)
                {
                  $this->arraydata['link']=baseinfo.'/portalshelf';
                }
                else
                {
                  $this->arraydata['link']=baseinfo.'?shelfid='.$arrayuser[0]['shelfid'];
                
                }
                
                if($this->post['uuid'])
                {
                $this->setuuidbyuserid($this->post['uuid'],$arrayuser[0]['userid']);
                
                }
                
                $this->logdata($arrayuser[0]['userid'],1);
                
               
               }else 
               {
                 
               $this->arraydata['error']=" Username หรือ Password ผิด "; 
               
               }
              
              }else
              {
              $this->arraydata['error']="ไม่มี username หรือ password ส่งขึ้นมา"; 
              
              }
           
           
           
           }
           
           function loginfacebook()
           {
           if($this->post['facebookid']&&$this->post['emailfacebook']&&$this->post['firstname']&&$this->post['shelfid'])
           {
           $sql="select userid,`status`,unlimited,oauth from tb_user where  tb_user.facebookid=".$this->post['facebookid'];
           
           $arrayuser=$this->db->db_set_recordset($sql); 
           if($arrayuser['0']['userid']=="")
           {
               $arrayuser=array();
               $arrayuser['0']['username']='facebook'.$this->post['facebookid'];
               //$arrayuser['email']=$this->post['email'][$k];
               if(!$this->checkuserbyemail($this->post['emailfacebook']))
               {
                  //$this->arraydata['error']="Email ซ้ำ";
                  $arrayuser['0']['email']=$this->post['emailfacebook'];
                 
               }
               $newfie=mktime().'-'.rand(0,100000).'.jpg'; 
               $arrayuser['0']['facebookid']=$this->post['facebookid'];
               $arrayuser['0']['emailfacebook']=$this->post['emailfacebook'];
               $arrayuser['0']['firstname']=$this->post['firstname'];
               $arrayuser['0']['updatedate']=date("Y-m-d H:i:s");
               $arrayuser['0']['createdate']=date("Y-m-d H:i:s");
               $arrayuser['0']['shelfid']=$this->post['shelfid'];
               $arrayuser['0']['photo']=$newfie; 
               $arrayuser['0']['password1']=md5(baseinfo.'-'.$this->post['facebookid']);
               $arrayuser['0']['oauth']=md5(baseinfo.'-'.$arrayuser['updatedate'].'-'.rand(0,1000000)); 
               $this->db->db_set($arrayuser['0'],"tb_user");
               $userid=$this->db->db_get_last_number(); 
                
                   if(!is_dir(imgpath.'/user'))
                   {
                   mkdir(imgpath.'/user',0777);
                   chmod(imgpath.'/user',0777);
                   }
                   
                   if(!is_dir(imgpath.'/user/'.$userid))
                   {
                   mkdir(imgpath.'/user/'.$userid,0777);
                   chmod(imgpath.'/user/'.$userid,0777);
                   }
               
                $file = imgpath.'/user/'.$userid.'/'.$newfie;
                $img = file_get_contents('https://graph.facebook.com/'.$arrayuser['facebookid'].'/picture?type=large');
                file_put_contents($file, $img);
                
                
                $unlimited=0;
                
                 
                
              
                
            
           
           
           }
           else
           {
           $userid=$arrayuser['0']['userid'];
           
           $unlimited=$arrayuser['0']['unlimited'];  
           
           
           }   
           
           
               if($this->post['rememberme'])
                {
                  setcookie("oauth",$arrayuser[0]['oauth'], time()+3600000, "/");
                }else
                {
                  setcookie("oauth",$arrayuser[0]['oauth'],0,'/'); 
                }
                
                $this->arraydata['oauth']=$arrayuser[0]['oauth'];
                $this->arraydata['unlimited']=$unlimited;   
                
                
                
                $this->logdata($userid,1);  
               
           
           }else
           {
           
           $this->arraydata['error']="ส่งข้อมูลไม่ครบ";
           }
           
           
           }
           
           function logout()
           {
               if($_COOKIE['oauth'])
               {
               $arrayuser=$this->getuserbyoauth($_COOKIE['oauth']);
               if($arrayuser['userid'])
               {
               $this->logdata($arrayuser['userid'],2);
               }
               setcookie("oauth",'', time()-3600000, "/");
               }
               
           
           }
           
           function validateuser()
           {
              if($this->post['username']&&$this->post['email']&&$this->post['password'])
              {
                 if($this->checkuserbyusername($this->post['username']))
                 {
                 $this->arraydata['error']="username ซ้ำ";
                 
                 }else if($this->checkuserbyemail($this->post['email']))
                 {
                  $this->arraydata['error']="Email ซ้ำ";
                 
                 }
              
              
              }else
              {
                $this->arraydata['error']="ข้อมูลไม่ครบ";
              
              }
           
           
           }
           
           function getShelf()
           {
                 if($this->get['shelfid']&&$this->get['uuid'])
                 {
                 $arraydata=$this->getshelfbyshelfid($this->get['shelfid']);
                 if($arraydata['photo'])
                 {
                 $arraydata['photo']=imginfo.'/'.$arraydata['shelfid'].'/'.$arraydata['photo'];
                 
                 }
                 $arraydata['banner']=$this->getbannerbyshelfid($this->get['shelfid']);
                 
                 $arraydata['sort']=$this->getsortlist($this->get['shelfid']);
                 
                 if(count($arraydata['banner']))
                 {
                 $k=0;
                 foreach($arraydata['banner'] as $valuebanner)
                 {
                 if($arraydata['banner'][$k]['photo'])
                 {
                  
                 $arraydata['banner'][$k]['photo']=imginfo.'/'.$arraydata['shelfid'].'/banner/'.$arraydata['banner'][$k]['photo'];
                 
                 }
                 
                 $k++;
                 }
                 
                 }else
                 {
                 unset($arraydata['banner']);
                 
                 }
                 $arraydata['bannerlibrary']=$this->getbannerlibrarybyshelfid($this->get['shelfid']);
                 if(count($arraydata['bannerlibrary']))
                 {
                 $k=0;
                 foreach($arraydata['bannerlibrary'] as $valuebanner)
                 {
                 if($arraydata['bannerlibrary'][$k]['photo'])
                 {
                  
                 $arraydata['bannerlibrary'][$k]['photo']=imginfo.'/'.$arraydata['shelfid'].'/banner/'.$arraydata['bannerlibrary'][$k]['photo'];
                 
                 }
                 
                 $k++;
                 }
                 
                 }else
                 {
                 unset($arraydata['bannerlibrary']);
                 
                 }
                 
                 $arraydata['tag']=$this->getalltagbyshelfid($this->get['shelfid']);
                 $arraydata['cat']=$this->getbooktypebyshelfid($this->get['shelfid']);
                 $arraydata['more']=$this->getmoreappbyshelfid($this->get['shelfid']);
                 
                 
                 $this->arraydata['data']=$arraydata;
                 $version=$this->getversion($this->get['shelfid']);
                 if($version)
                 {
                 $this->arraydata['version']=$version;
                 }else
                 {
                 $this->arraydata['version']=1;
                 }
                 
                 
                 $this->logdatamobile($this->get['uuid'],3,$this->info['parameter'][0],$this->get['shelfid']);
                 }
                 else
                 {
                 $this->arraydata['error']="ไม่มี shelfid หรือ uuid";
                 }
           
           }
           
           function checkdelbook()
           {
           if($this->get['shelfid']&&$this->get['createdate'])
           {
           
           
            $updatedate=date("Y-m-d H:i:s",$this->get['createdate']);
            $sql="select bookid from tb_del_book where tb_del_book.shelfid=".$this->get['shelfid']." and   tb_del_book.createdate<'".$updatedate."'";
            $arraydata=$this->db->db_set_recordset($sql);
            $this->arraydata['data']=$arraydata;
           
           }else
           {
           $this->arraydata['error']="ส่งข้อมูลไม่ครบ";
           
           }
           
           
           }
           
           function getAllBookbyShelfid()
           {
           if($this->get['shelfid']&&$this->get['page']&&$this->get['uuid']&&$this->get['oauth'])
           {
           $strdataquery=http_build_query($this->get);
           $arraydata=$this->memcache->get($strdataquery);
           if(count($arraydata)==0)
           {
           $page=$this->get['page'];
           $page=(($page - 1) * 10);
            $innerjoin="";
            $where="";
            if($this->get['tagid'])
            {
            $innerjoin.=" INNER JOIN tb_tag_relate ON tb_book.bookid = tb_tag_relate.bookid "; 
            $where.=" and  tb_tag_relate.tagid=".$this->get['tagid'];
            $groupby=" group by tb_book.bookid ";
            }
            
            
            if($this->get['oauth'])
            {
            if($this->checkadminbyoauth($this->get['oauth']))
            {
            
            
            }else
            {
            $innerjoin.=" INNER JOIN tb_user_relate ON tb_book.bookid = tb_user_relate.bookid "; 
            $innerjoin.=" INNER JOIN tb_user ON tb_user.userid = tb_user_relate.userid "; 
            
            $where.=" and  tb_user.oauth='".$this->get['oauth']."' ";
            
            $groupby=" group by tb_book.bookid ";
            
            
            
            }
            
           
           }
            
            if($this->get['catid'])
            {
            $innerjoin.=" INNER JOIN tb_cat_relate ON tb_book.bookid = tb_cat_relate.bookid "; 
            $where.=" and  tb_cat_relate.catid=".$this->get['catid'];
            $groupby=" group by tb_book.bookid ";
            }
            
            if($this->get['search'])
            {
           // $innerjoin.=" INNER JOIN tb_cat_relate ON tb_book.bookid = tb_cat_relate.bookid "; 
            $where.=" and  ( tb_book.title like '%".$this->get['search']."%' or tb_book.description like '%".$this->get['search']."%' or tb_book.keyword like '%".$this->get['search']."%' or tb_book.topic like '%".$this->get['search']."%' )";
            //$groupby=" group by tb_book.bookid ";
            }
            
            
            if($this->get['updatedate'])
            {
            $updatedate=date("Y-m-d H:i:s",$this->get['updatedate']);
            $where.=" and  tb_book.updatedate<'".$updatedate."'";
            
            }
            $orderby="tb_book.date desc";
            
            
            
                    
            if($this->get['orderby'])
            {
            
                    
                    
            $orderby=str_replace("%20"," ",$this->get['orderby']) ;
            
            }
            
            
            
              if($this->get['orderby'])
             {
             switch (intval($this->get['orderby'])) {
                case 1:
                    $setorder="tb_book.date desc,tb_book.time desc";
                    break;
                case 2:
                    $setorder="tb_book.date asc,tb_book.time asc";
                    break;
                case 3:
                    $setorder="tb_book.title asc";
                    break;
                case 5:
                    $setorder="date desc,TIMESTAMP(date,time) asc";
                    break;
                default:
                    $setorder="tb_book.createdate desc";
                    break;

            }
             $orderby=" order by ".$setorder;
             
             }else
             {
             $orderby=" order by tb_book.date desc,tb_book.time desc";
             }
            
            
           $sql="select tb_book.bookid from tb_book $innerjoin where  tb_book.shelfid=".$this->get['shelfid']."   $where $groupby order by $orderby  limit  $page,10 ";
           
           if($_GET['admin'])
           {
         // echo $sql;
           }
           

           
           $arraybook=$this->db->db_set_recordset($sql);
           $arraydata=array();
           
           if(count($arraybook))
           {
           foreach($arraybook as $valuebook)
           {
           $setnow=$this->getbookbybookid($valuebook['bookid'],1,$this->get['uuid']);
           $arraydata[]=$setnow['book'];
           
           }
           
           
           }
           $this->memcache->set($strdataquery,$arraydata);
           }
           
           $this->arraydata['data']=$arraydata;
           
           $this->logdatamobile($this->get['uuid'],3,$this->info['parameter'][0],$this->get['shelfid']);
           
           }else
           {
           $this->arraydata['error']="ส่งข้อมูลไม่ครบ";
           
           }
           
           }
           
           function getsqldata()
           {
           $sql="SELECT
tb_user_copy1.username,
tb_user_copy1.email,
tb_user_copy1.firstname,
tb_user.username as username1,
tb_user.userid
FROM
tb_user_copy1
INNER JOIN tb_user ON tb_user_copy1.firstname = tb_user.firstname
where tb_user.firstname<>'' ";
            $arraydata=$this->db->db_set_recordset($sql);
            echo "<pre>";
            print_r($arraydata);
            echo "</pre>;";
            if(count($arraydata))
            {
            foreach($arraydata as $valuedata)
            {
            $arraynewdata=array();
            $arraynewdata['username']=$valuedata['username'];
            $arraynewdata['email']=$valuedata['email'];
            $this->db->db_set($arraynewdata,'tb_user',' tb_user.userid='.$valuedata['userid']);
            
            
            }
            
            }
           
           }
           
           function checkphpversion()
           {
            phpinfo();
            exit;
           
           }
           
           function getDoc()
           {
            if ($this->get['docid'])
            {
            $this->db->get_connect();
            $sql="SELECT
                 *
                FROM
                info_title
                INNER JOIN info_doc ON info_title.tid = info_doc.tid
                where  info_doc.docid=" . $this->get['docid'] . "
                ";
            $arraydata=$this->db->db_set_recordset($sql);
            

            $arraynewdata=array();

            if (count($arraydata))
                {
                //$k=0;
                $this->arraydata['data']=$arraydata[0];
                }

            $sql="SELECT * FROM info_parem where info_parem.docid=" . $this->get['docid'];
            $arraydata=$this->db->db_set_recordset($sql);

            

            if (count($arraydata))
                {
                //$k=0;
                $this->arraydata['key']=$arraydata;
                }

           
            }
        
           }
           
           function savebook()
           {
               
               if(($this->checkuser($this->post['shelfid'])&&$this->post['shelfid'])||($this->checkadmin()))
               {
               
               //$pattern = '/[a-z0-9_\-\+]+@[a-z0-9\-]+\.([a-z]{2,3})(?:\.[a-z]{2})?/i';

// preg_match_all returns an associative array
//            preg_match_all($pattern, $this->post['userid'], $matches);
            
            
            
            
                 $pattern="/(?:[a-z0-9!#$%&'*+=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+=?^_`{|}~-]+)*|\"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/";

//$pattern="/[A-Za-z0-9_-]+@[A-Za-z0-9_-]+\.([A-Za-z0-9_-][A-Za-z0-9_]+)/";




preg_match_all($pattern, $this->post['userid'], $matches);

                  
                  $this->post['userid']=$matches[0];
                  
                  
                  
                  
                  // exit;
                    
                   //$this->arraydata['post']=$this->post;
                   //exit;
                   $userdataarray=$this->getuserbyoauth($_COOKIE['oauth']);
                   $arraydata=array();
                   $shelfid=$this->post['shelfid'];
                   $bookid=$this->post['bookid'];
                   $arraydata['shelfid']=$shelfid;
                   $arraydata['photo']=$this->post['photo'];
                   $arraydata['title']=$this->post['title'];
                   $arraydata['description']=$this->post['description'];
                   $arraydata['keyword']=$this->post['keyword'];
                   $arraydata['status']=$this->post['status'];
                   $arraydata['topic']=$this->post['topic'];
                   
                   if($this->post['date'])
                   {
                      list($day,$month,$year)=explode("/",$this->post['date']);
                      
                      $arraydata['date']=$year.'-'.$month.'-'.$day;
                   
                   }
                   
                   $arraydata['time']=$this->post['time'].':00';
                   $arraydata['time2']=$this->post['time2'].':00';
                   
                   
                   $arraydata['place']=$this->post['place'];
                   
                   
                   $arraydata['shelfid']=$this->post['shelfid'];
                   $arraydata['updatedate']=date("Y-m-d H:i:s");
                   $arraydata['updateby']=$userdataarray['userid'];
                   
               if($bookid)
               { 
               if($this->post['photo'])
               {
               $sql="select bookid from tb_book where photo='".$this->post['photo']."' and bookid=$bookid and shelfid=$shelfid ";
               $arraybook=$this->db->db_set_recordset($sql);
               if(!$arraybook[0]['bookid']&&$this->post['photo'])
               {
                copy(cacheimgpath.'/'.$this->post['photo'],imgpath.'/'.$shelfid.'/'.$bookid.'/'.$this->post['photo']);
                chmod(imgpath.'/'.$shelfid.'/'.$bookid.'/'.$this->post['photo'],0777);
               
               }
               }
               
               $this->db->db_set($arraydata,"tb_book",' bookid='.$bookid); 
               $this->setversionbook($bookid);
               
               
               $version=$this->getversion($shelfid);
               if($version)
               {
               
             //  $arrayversion['shelfid']=$shelfid;
               $arrayversion['version']=$version+1;
               $this->db->db_set($arrayversion,"tb_version",' shelfid='.$shelfid);
               
               }else
               {
               $arrayversion['shelfid']=$shelfid;
               $arrayversion['version']=1;
               $this->db->db_set($arrayversion,"tb_version");
               
               }
               
               
               }
               else
               {
               $set=1;
               $arraydata['createby']=$userdataarray['userid'];
               $arraydata['createdate']=date("Y-m-d H:i:s");
               $this->db->db_set($arraydata,"tb_book");
               $bookid=$this->db->db_get_last_number();
               
               }
               
               
               
               if($set)
               {
               
               if(!is_dir(imgpath.'/'.$shelfid.'/'.$bookid))
               {
               mkdir(imgpath.'/'.$shelfid.'/'.$bookid,0777);
               chmod(imgpath.'/'.$shelfid.'/'.$bookid,0777);
               }
               
               
               if(!is_dir(imgpath.'/'.$shelfid.'/'.$bookid.'/banner'))
               {
               mkdir(imgpath.'/'.$shelfid.'/'.$bookid.'/banner',0777);
               chmod(imgpath.'/'.$shelfid.'/'.$bookid.'/banner',0777);
               }
               
               if(!is_dir(filepath.'/'.$shelfid))
               {
               mkdir(filepath.'/'.$shelfid,0777);
               chmod(filepath.'/'.$shelfid,0777);
               }
               
               if(!is_dir(filepath.'/'.$shelfid.'/'.$bookid))
               {
               mkdir(filepath.'/'.$shelfid.'/'.$bookid,0777);
               chmod(filepath.'/'.$shelfid.'/'.$bookid,0777);
               }
               
               
               
               if($this->post['photo'])
               {
               copy(cacheimgpath.'/'.$this->post['photo'],imgpath.'/'.$shelfid.'/'.$bookid.'/'.$this->post['photo']);
               chmod(imgpath.'/'.$shelfid.'/'.$bookid.'/'.$this->post['photo'],0777);
               }
               
               
               
               
             
               
               }
               
             
               
               
               $sql="delete from tb_tag_relate where bookid=".$bookid; 
               $this->db->db_delete($sql); 
               if(count($this->post['tagid']))
               {
               foreach($this->post['tagid'] as $valuetag)
               {
               
               $arraytag['tagid']=$valuetag;
               $arraytag['bookid']=$bookid;
               $arraytag['createdate']=date("Y-m-d H:i:s");
               $this->db->db_set($arraytag,"tb_tag_relate");
               
               }
               
               
               }
               
               $sql="delete from tb_cat_relate where bookid=".$bookid; 
               $this->db->db_delete($sql); 
               if(count($this->post['catid']))
               {
               foreach($this->post['catid'] as $valuecat)
               {
               
               $arraycat['catid']=$valuecat;
               $arraycat['bookid']=$bookid;
               $arraycat['createdate']=date("Y-m-d H:i:s");
               $this->db->db_set($arraycat,"tb_cat_relate");
               
               }
               
               
               }
               
               $sql="delete from tb_user_relate where bookid=".$bookid; 
               $this->db->db_delete($sql); 
               $arrayuserid=array();
               if(count($this->post['userid']))
               {
               foreach($this->post['userid'] as $valueuser)
               {
               $sql="select *  from tb_user where tb_user.email='".$valueuser."' or tb_user.username='".$valueuser."' "; 
               $arrayusercheck=$this->db->db_set_recordset($sql);
               
               if($arrayusercheck[0]['userid'])
               {
               
               
               $arrayuserid[]=$arrayusercheck[0]['userid'];
               $arrayuser['userid']=$arrayusercheck[0]['userid'];
               $arrayuser['bookid']=$bookid;
               $arrayuser['createdate']=date("Y-m-d H:i:s");
               $this->db->db_set($arrayuser,"tb_user_relate");
               }
               
               }
               }
               
               
               $sql="select *  from tb_file where bookid=".$bookid; 
                $arrayfile=$this->db->db_set_recordset($sql);
                $arraynewfile=array();
                $arraynewfileimg=array();
                if(count($arrayfile))
                {
                foreach($arrayfile as $valuefile)
                {
                
                $arraynewfile[]=$valuefile['file'];
                $arraynewfileimg[]=$valuefile['img'];
                
                
                }
                
                
                }
               
                
               if(count($this->post['files']))
               {
               $k=0;
               $arraynewfile2=array();
               $arraynewfileimg2=array();
               foreach($this->post['files'] as $valuefile)
               {
               $arrayfile=array();
               $arrayfile['name']=$valuefile['name'];
               $arrayfile['sort']=$valuefile['sort'];
               $arrayfile['group']=$valuefile['group'];
               $arrayfile['img']=$valuefile['img'];
               $arraynewfile2[]=$valuefile['file'];
               $arraynewfileimg2[]=$valuefile['img'];
               
               
               if($valuefile['img'])
               {
               if(!is_file(imgpath.'/'.$shelfid.'/'.$bookid.'/banner/'.$valuefile['img']))
               {
               @copy(cacheimgpath.'/'.$valuefile['img'],imgpath.'/'.$shelfid.'/'.$bookid.'/banner/'.$valuefile['img']);
               @chmod(imgpath.'/'.$shelfid.'/'.$bookid.'/banner/'.$valuefile['img'],0777);
               
               }
               }
               
               if(!is_file(filepath.'/'.$shelfid.'/'.$bookid.'/'.$valuefile['file']))
               {

               @copy(cacheimgpath.'/'.$valuefile['file'],filepath.'/'.$shelfid.'/'.$bookid.'/'.$valuefile['file']);
               @chmod(filepath.'/'.$shelfid.'/'.$bookid.'/'.$valuefile['file'],0777);
               
               $arrayfile['file']=$valuefile['file'];
               $arrayfile['typeid']=$valuefile['type'];
               $arrayfile['bookid']=$bookid;
               $this->db->db_set($arrayfile,"tb_file");
               
               }else
               {
               
               $this->db->db_set($arrayfile,"tb_file"," tb_file.file='".$valuefile['file']."'");
               
               }
               
               
               
               
               
               
               
               
               
               $k++;
               
               }
              
                

               
               
               }
               
               if(count($arraynewfile))
               {
               foreach($arraynewfile as $valuenewfile2)
               {
               
               if(!in_array($valuenewfile2,$arraynewfile2))
               {
               @unlink(filepath.'/'.$shelfid.'/'.$bookid.'/'.$valuenewfile2);
               $sql="delete from tb_file where tb_file.file='".$valuenewfile2."'"; 
               $this->db->db_delete($sql); 
               
               }
               
               
               }
               
               
               }
               
               if(count($arraynewfileimg))
               {
               foreach($arraynewfileimg as $valuenewfileimg2)
               {
               
               if(!in_array($valuenewfileimg2,$arraynewfileimg2))
               {
               @unlink(imgpath.'/'.$shelfid.'/'.$bookid.'/banner/'.$valuenewfileimg2);
               
               
               }
               
               
               }
               
               
               }
               
               if($this->post['status']==2&&count($this->post['userid']))
               {
                $arraydata=array();
                $arrayshelf=$this->getshelfbyshelfid($shelfid);
                
                $pem=filepath.'/'.$shelfid.'/'.$arrayshelf['pemfile'];
                $pass=$arrayshelf['passpem'];
                
                $arraytoken=$this->gettokenbyuserids($arrayuserid);
                $msg="New Document is avaliable for download";
                if(count($arraytoken)&&is_file($pem)&&$pass)
                {
                $k=0;
                
                foreach($arraytoken as $valuetoken)
                {
                $arraydata[$k]['token']=$valuetoken['token'];
                $arraydata[$k]['alert']=$msg;
                $arraydata[$k]['badge']=$valuetoken['badge']+1;
                $arraydata[$k]['type']=1;
                
                $arrayset['badge']=$arraydata[$k]['badge'];
                $arrayset['updatedate']=date("Y-m-d H:i:s");
                
                $this->db->db_set($arrayset,"tb_token",' tokenid='.$valuetoken['tokenid']);
                
                $k++;
                }
                
                
                }

                $this->pushnotification($arraydata,$pem,$pass);
                //$this->arraydata['error']="คุณไม่ช่าย User ของ shelf นี้";
               
               
               }
               
                             
               
                  
                  
                  
                  }
               else
               {
               $this->arraydata['error']="คุณไม่ช่าย User ของ shelf นี้";
               }
           
           } 
           
           function cleanmeetingdata()
            {
                       $sql='SELECT
tb_agency.name_agency,
tb_user_bk.username,
tb_create_meeting.name_meeting_upload,
tb_create_meeting.meeting_file_text,
tb_create_meeting.meeting_permit,
tb_create_meeting.meeting_room,
tb_create_meeting.meeting_time,
tb_create_meeting.meeting_date,
tb_create_meeting.meeting_name_document,
tb_create_meeting.meeting_tag,
tb_create_meeting.meeting_title_name
FROM
tb_agency
INNER JOIN tb_create_meeting ON tb_create_meeting.id_agency = tb_agency.id
INNER JOIN tb_user_bk ON tb_create_meeting.uid = tb_user_bk.uid
';
                 $arraydata=$this->db->db_set_recordset($sql);  
                 $userdataarray=$this->getuserbyoauth($_COOKIE['oauth']);  
                 if(count($arraydata)) 
                 {       $arraysearch=array('น.','เวลา','o.');    
                     foreach($arraydata as $valuedata)
                     {
                         
                             $arraydata2=array();  
                             $arraydata2['topic']=$valuedata['meeting_title_name'];
                             $arraydata2['title']=$valuedata['meeting_name_document'];
                             $arraydata2['description']=$valuedata['meeting_title_name'];
                             $arraydata2['date']=$valuedata['meeting_date'];
                             
                             if($valuedata['meeting_time'])
                             {
                                 
                                 $valuedata['meeting_time']= str_replace($arraysearch,'',$valuedata['meeting_time']) ; 
                                 $valuedata['meeting_time']= str_replace('.',':',$valuedata['meeting_time']) ; 
                                  list($time,$time2)=explode("-",$valuedata['meeting_time'])  ;
                                  $arraydata2['time']=trim($time).':'.'00';
                                  $arraydata2['time2']=trim($time2).':'.'00';   
                                  
                                  
                                  
                                 
                             } 
                            
                             $arraydata2['place']=$valuedata['meeting_room'];                                                           
                             $arraydata2['date']=$valuedata['meeting_date'];
                             $arraydata2['status'] =2 ;
                             
                             
                             $set=1;
                             $arraydata2['createby']=$userdataarray['userid'];
                             $arraydata2['createdate']=date("Y-m-d H:i:s");

                             $this->db->db_set($arraydata2,"tb_book");
                             $bookid=$this->db->db_get_last_number();
                             
                             
                             $sql="select * from tb_tag where name like '%".trim($valuedata['name_agency'])."%' " ;
                             
                             $arraytag2=$this->db->db_set_recordset($sql);     
                             
                             $shelfid=1;
                             
                 if($set)
               {
               
               if(!is_dir(imgpath.'/'.$shelfid.'/'.$bookid))
               {
               mkdir(imgpath.'/'.$shelfid.'/'.$bookid,0777);
               chmod(imgpath.'/'.$shelfid.'/'.$bookid,0777);
               }
               
               
               if(!is_dir(imgpath.'/'.$shelfid.'/'.$bookid.'/banner'))
               {
               mkdir(imgpath.'/'.$shelfid.'/'.$bookid.'/banner',0777);
               chmod(imgpath.'/'.$shelfid.'/'.$bookid.'/banner',0777);
               }
               
               if(!is_dir(filepath.'/'.$shelfid))
               {
               mkdir(filepath.'/'.$shelfid,0777);
               chmod(filepath.'/'.$shelfid,0777);
               }
               
               if(!is_dir(filepath.'/'.$shelfid.'/'.$bookid))
               {
               mkdir(filepath.'/'.$shelfid.'/'.$bookid,0777);
               chmod(filepath.'/'.$shelfid.'/'.$bookid,0777);
               }
               
               
               
               if($this->post['photo'])
               {
               copy(cacheimgpath.'/'.$this->post['photo'],imgpath.'/'.$shelfid.'/'.$bookid.'/'.$this->post['photo']);
               chmod(imgpath.'/'.$shelfid.'/'.$bookid.'/'.$this->post['photo'],0777);
               }
               
               }
               
               

               
                     
               if(count($arraytag2))
               {
               foreach($arraytag2 as $valuetag)
               {
               
               $arraytag['tagid']=$valuetag['tagid'];
               $arraytag['bookid']=$bookid;
               $arraytag['createdate']=date("Y-m-d H:i:s");
               $this->db->db_set($arraytag,"tb_tag_relate");
               
               }
               }
               
               $this->post['catid']=array();
               $this->post['catid'][]=1;
               if(count($this->post['catid']))
               {
               foreach($this->post['catid'] as $valuecat)
               {
               
               $arraycat['catid']=$valuecat;
               $arraycat['bookid']=$bookid;
               $arraycat['createdate']=date("Y-m-d H:i:s");
               $this->db->db_set($arraycat,"tb_cat_relate");
               
               }
               
               
               }
               
               
                                  $this->post['userid']=array();
                  $pattern = '/[a-z0-9_\-\+]+@[a-z0-9\-]+\.([a-z]{2,3})(?:\.[a-z]{2})?/i';

// preg_match_all returns an associative array
                preg_match_all($pattern, $valuedata['meeting_permit'], $matches);

                  
                  $this->post['userid']=$matches[0];
                  
                  
               if(count($this->post['userid']))
               {
               foreach($this->post['userid'] as $valueuser)
               {
               $sql="select *  from tb_user where tb_user.email='".trim($valueuser)."' or tb_user.username='".trim($valueuser)."' "; 
               $arrayusercheck=$this->db->db_set_recordset($sql);
               
               if($arrayusercheck[0]['userid'])
               {
               
               
               $arrayuserid[]=$arrayusercheck[0]['userid'];
               $arrayuser['userid']=$arrayusercheck[0]['userid'];
               $arrayuser['bookid']=$bookid;
               $arrayuser['createdate']=date("Y-m-d H:i:s");
               $this->db->db_set($arrayuser,"tb_user_relate");
               }
               
               }
               }
               
                       if($valuedata['name_meeting_upload'])
                       {
                       $arrayfile=explode('[*]',$valuedata['name_meeting_upload']) ;
                       $arraytext=explode('[*]',$valuedata['meeting_file_text']) ; 
                       
                       if(count($arrayfile))
                       {
                            $sort=1;
                            $rowk=0;
                           foreach($arrayfile as $valuefile)
                           {
                               $newname=mktime().'-'.rand(0,100000).'.pdf';
                               if(is_file(rootpath.'/assets/test/user_c/'.$valuedata['username'].'/image_c/'.$valuefile))
                               {
                               if(copy(rootpath.'/assets/test/user_c/'.$valuedata['username'].'/image_c/'.$valuefile,filepath.'/'.$shelfid.'/'.$bookid.'/'.$newname))
                               {
                                          $arrayfile2=array();
                                          
                                            $arrayfile2['name']=$arraytext[$rowk];
               $arrayfile2['sort']=$sort;
               $arrayfile2['group']=$sort;
               $arrayfile2['img']='';
               $arrayfile2['file']=$newname;
               $arrayfile2['bookid']=$bookid;
               
               $arrayfile2['typeid']=1;
               $arrayfile2['bookid']=$bookid;
               $this->db->db_set($arrayfile2,"tb_file");
                                          
                                   
                               } 
                               }else
                               {
                                     $valuefile=str_replace('.','',$valuefile);
                                     
                               if(is_file(rootpath.'/assets/test/user_c/'.$valuedata['username'].'/image_c/'.trim($valuefile)))
                               {
                               if(copy(rootpath.'/assets/test/user_c/'.$valuedata['username'].'/image_c/'.$valuefile,filepath.'/'.$shelfid.'/'.$bookid.'/'.$newname))
                               {
                                          $arrayfile2=array();
                                          
                                            $arrayfile2['name']=$arraytext[$rowk];
               $arrayfile2['sort']=$sort;
               $arrayfile2['group']=$sort;
               $arrayfile2['img']='';
               $arrayfile2['file']=$newname;
               $arrayfile2['bookid']=$bookid;
               
               $arrayfile2['typeid']=1;
               $arrayfile2['bookid']=$bookid;
               $this->db->db_set($arrayfile2,"tb_file");
                                          
                                   
                               } 
                               
                               }
                                   
                                   
                               }
                               $rowk++;
                               
                           }
                           
                       }
               
                       }
               
                             
                         
                     }
                     
                     
                 }
                
                
            }
            
            function cleanmeetingdata2()
            {
                       $sql='SELECT
tb_create_meeting.id,
tb_book.bookid,
tb_create_meeting.meeting_permit
FROM
tb_book
INNER JOIN tb_create_meeting ON tb_book.topic = tb_create_meeting.meeting_title_name AND tb_book.title = tb_create_meeting.meeting_name_document AND tb_book.date = tb_create_meeting.meeting_date
order by tb_book.bookid desc
';
                 $arraydata=$this->db->db_set_recordset($sql);  
                 $userdataarray=$this->getuserbyoauth($_COOKIE['oauth']);  
                 if(count($arraydata)) 
                 {      // $arraysearch=array('น.','เวลา','o.');    
                     foreach($arraydata as $valuedata)
                     {
                         

                            

                             

               
               $pattern="/(?:[a-z0-9!#$%&'*+=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+=?^_`{|}~-]+)*|\"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/";

//$pattern="/[A-Za-z0-9_-]+@[A-Za-z0-9_-]+\.([A-Za-z0-9_-][A-Za-z0-9_]+)/";




            preg_match_all($pattern, $valuedata['meeting_permit'], $matches);

                  
                  $this->post['userid']=$matches[0];

               
                     



               

               $sql="delete from tb_user_relate where bookid=".$valuedata['bookid']; 
               $this->db->db_delete($sql);    
                  
               if(count($this->post['userid']))
               {
               foreach($this->post['userid'] as $valueuser)
               {
               $sql="select *  from tb_user where tb_user.email='".trim($valueuser)."' or tb_user.username='".trim($valueuser)."' "; 
               $arrayusercheck=$this->db->db_set_recordset($sql);
               
               if($arrayusercheck[0]['userid'])
               {
               
               
               $arrayuserid[]=$arrayusercheck[0]['userid'];
               $arrayuser['userid']=$arrayusercheck[0]['userid'];
               $arrayuser['bookid']=$valuedata['bookid'];
               $arrayuser['createdate']=date("Y-m-d H:i:s");
               $this->db->db_set($arrayuser,"tb_user_relate");
               }
               
               }
               }
               

               
                             
                         
                     }
                     
                     
                 }
                
                
            }
           
           function savesetting()
           {
           
           if((($this->checkuser($this->post['shelfid']))||($this->checkadmin()))&&$this->post['shelfid'])
               {
                  $check=1;
                   $userdataarray=$this->getuserbyoauth($_COOKIE['oauth']);
                   
                   if($userdataarray['email']!=$this->post['email'])
                   {
                   
                       if($this->checkuserbyemail($this->post['email']))
                         {
                          $this->arraydata['error']="Email ซ้ำ";
                          $check=0;
                         
                         }
                   
                   }
                   if($this->post['password1'])
                   {
                   $arraydata['password1']=md5(baseinfo.'-'.$this->post['password1']);
                   
                   }
                   
                   
                   if($userdataarray['username']!=$this->post['username'])
                   {
                   
                       if($this->checkuserbyusername($this->post['username']))
                         {
                          $this->arraydata['error']="Username ซ้ำ";
                          $check=0;
                         
                         }
                   
                   }
                   
                   if($check)
                   {
                   $shelfid=$this->post['shelfid'];
                 
                 //  $arraydata['shelfid']=$shelfid;
                   if($this->post['photo'])$arraydata['photo']=$this->post['photo'];
                   
                   $arraydata['username']=$this->post['username'];
                   $arraydata['firstname']=$this->post['firstname'];
                   $arraydata['email']=$this->post['email'];
                   $arraydata['updatedate']=date("Y-m-d H:i:s");
                   
                   
                    if(!is_dir(imgpath.'/user'))
                   {
                   mkdir(imgpath.'/user',0777);
                   chmod(imgpath.'/user',0777);
                   }
                   
                   if(!is_dir(imgpath.'/user/'.$userdataarray['userid']))
                   {
                   mkdir(imgpath.'/user/'.$userdataarray['userid'],0777);
                   chmod(imgpath.'/user/'.$userdataarray['userid'],0777);
                   }
                   
                   
                   if(!is_file(imgpath.'/user/'.$userdataarray['userid'].'/'.$this->post['photo'])&&$this->post['photo'])
                   {
                   copy(cacheimgpath.'/'.$this->post['photo'],imgpath.'/user/'.$userdataarray['userid'].'/'.$this->post['photo']);
                   chmod(imgpath.'/user/'.$userdataarray['userid'].'/'.$this->post['photo'],0777);
                   }
                   
                   $this->db->db_set($arraydata,"tb_user",' userid='.$userdataarray['userid']);
                   
                   }
               
               
               
               }
               else
               {
              $this->arraydata['error']=" ข้อมูลไม่ครบ "; 
               
               }
           
           
           }
           
           
           
            function savesettingdatauser()
           {
           
           if((($this->checkadmin()))&&$this->post['userid'])
               {
                  $check=1;
                   $userdataarray=$this->getuserbyuserid($this->post['userid']);
                   
                   if($userdataarray['email']!=$this->post['email'])
                   {
                   
                       if($this->checkuserbyemail($this->post['email']))
                         {
                          $this->arraydata['error']="Email ซ้ำ";
                          $check=0;
                         
                         }
                   
                   }
                   if($this->post['password1'])
                   {
                   $arraydata['password1']=md5(baseinfo.'-'.$this->post['password1']);
                   
                   }
                   
                   
                   if($userdataarray['username']!=$this->post['username'])
                   {
                   
                       if($this->checkuserbyusername($this->post['username']))
                         {
                          $this->arraydata['error']="Username ซ้ำ";
                          $check=0;
                         
                         }
                   
                   }
                   
                   if($check)
                   {
                   $shelfid=$this->post['shelfid'];
                 
                 //  $arraydata['shelfid']=$shelfid;
                   if($this->post['photo'])$arraydata['photo']=$this->post['photo'];
                   
                   //$arraydata['username']=$this->post['username'];
                   $arraydata['firstname']=$this->post['firstname'];
                   $arraydata['email']=$this->post['email'];
                   $arraydata['updatedate']=date("Y-m-d H:i:s");
                   
                   
                    if(!is_dir(imgpath.'/user'))
                   {
                   mkdir(imgpath.'/user',0777);
                   chmod(imgpath.'/user',0777);
                   }
                   
                   if(!is_dir(imgpath.'/user/'.$userdataarray['userid']))
                   {
                   mkdir(imgpath.'/user/'.$userdataarray['userid'],0777);
                   chmod(imgpath.'/user/'.$userdataarray['userid'],0777);
                   }
                   
                   
                   if(!is_file(imgpath.'/user/'.$userdataarray['userid'].'/'.$this->post['photo'])&&$this->post['photo'])
                   {
                   copy(cacheimgpath.'/'.$this->post['photo'],imgpath.'/user/'.$userdataarray['userid'].'/'.$this->post['photo']);
                   chmod(imgpath.'/user/'.$userdataarray['userid'].'/'.$this->post['photo'],0777);
                   }
                   
                   $this->db->db_set($arraydata,"tb_user",' userid='.$userdataarray['userid']);
                   
                   }
               
               
               
               }
               else
               {
              $this->arraydata['error']=" ข้อมูลไม่ครบ "; 
               
               }
           
           
           }
           
           
           
           function savesettingmobile()
           {
           
               if(!$this->post['oauth'])
               {
               $this->arraydata['error']="ไม่ได้ส่ง oauth";
               
               }else
               {
               $_COOKIE['oauth']=$this->post['oauth'];
               if((($this->checkuser($this->post['shelfid'])&&$this->post['shelfid'])||($this->checkadmin())))
               {
                     $check=1;
                    $userdataarray=$this->getuserbyoauth($_COOKIE['oauth']);

                    
                    
                     if($userdataarray['email']!=$this->post['email'])
                   {
                   
                       if($this->checkuserbyemail($this->post['email']))
                         {
                          $this->arraydata['error']="Email ซ้ำ";
                          $check=0;
                         
                         }
                   
                   }
                   
                   if($userdataarray['username']!=$this->post['username'])
                   {
                   
                       if($this->checkuserbyusername($this->post['username']))
                         {
                          $this->arraydata['error']="Username ซ้ำ";
                          $check=0;
                         
                         }
                   
                   }
                   
                   
                   $arraydata=array();
                   $shelfid=$this->post['shelfid'];

                   
                   if($check)
                   {
                   if(!is_dir(imgpath.'/user'))
                   {
                   mkdir(imgpath.'/user',0777);
                   chmod(imgpath.'/user',0777);
                   }
                   
                   if(!is_dir(imgpath.'/user/'.$userdataarray['userid']))
                   {
                   mkdir(imgpath.'/user/'.$userdataarray['userid'],0777);
                   chmod(imgpath.'/user/'.$userdataarray['userid'],0777);
                   }
                   
 
                    
                    
                   
                      if(base64_decode($this->post['photo'],true)) 
                      {
                      $newfie=mktime().'-'.rand(0,100000).'.jpg';
                      $arraydata['photo']=$newfie;
                      $fp = fopen(imgpath.'/user/'.$userdataarray['userid'].'/'.$newfie, 'wb');
                      fwrite($fp, base64_decode($this->post['photo']));
                      fclose($fp);
                      chmod(imgpath.'/user/'.$userdataarray['userid'].'/'.$newfie,0777);
                      }
                      
                   
                    
                   if($this->post['password1'])
                   {
                   $password1=md5(baseinfo.'-'.$this->post['password1']);
                   $arraydata['password1']=$password1;
                   
                   }
                  
                   $arraydata['username']=$this->post['username'];
                   $arraydata['email']=$this->post['email'];
                   $arraydata['updatedate']=date("Y-m-d H:i:s"); 
                   
                   $this->db->db_set($arraydata,"tb_user",' userid='.$userdataarray['userid']); 
                   }
                  
                  
                  
               }
               else
               {
               $this->arraydata['error']="คุณไม่ช่าย User ของ shelf นี้ หรือ photo ไม่ช่าย base64";
               }
               }
           
           }
           
           function removebook()
           {
           if(($this->checkuser($this->post['shelfid'])||$this->checkadmin())&&$this->post['shelfid']&&$this->post['bookid'])
           {
             require_once(rootpath.'/library/plugins/general_pi.php'); // uri
             
              $userdataarray=$this->getuserbyoauth($_COOKIE['oauth']);
           
              $sql="select bookid from tb_book where shelfid=".$this->post['shelfid'].' and bookid='.$this->post['bookid'].' and createby='.$userdataarray['userid'];
              $arraybook=$this->db->db_set_recordset($sql);
              
              if($arraybook[0]['bookid']||$this->checkadmin())
              {
              
              if($this->checkadmin())
              {
              $arraybook[0]['bookid']=$this->post['bookid'];
              
              }
              
              $sql="delete from tb_book where shelfid=".$this->post['shelfid'].' and bookid='.$arraybook[0]['bookid']; 
              $this->db->db_delete($sql);
              
              $sql="delete from tb_tag_relate where   bookid=".$arraybook[0]['bookid']; 
              $this->db->db_delete($sql);
              
              $sql="delete from tb_cat_relate where   bookid=".$arraybook[0]['bookid']; 
              $this->db->db_delete($sql);
              
              
              $sql="delete from tb_user_relate where   bookid=".$arraybook[0]['bookid']; 
              $this->db->db_delete($sql);
              
              
              $sql="delete from tb_file where   bookid=".$arraybook[0]['bookid']; 
              $this->db->db_delete($sql);
              
              $arraydata['bookid']=$arraybook[0]['bookid'];
              $arraydata['shelfid']=$this->post['shelfid'];
              $arraydata['createdate']=date("Y-m-d H:i:s");
              $this->db->db_set($arraydata,"tb_del_book");
              
              deleteAll(imgpath.'/'.$this->post['shelfid'].'/'.$arraybook[0]['bookid']); 
              @deleteAll(filepath.'/'.$this->post['shelfid'].'/'.$arraybook[0]['bookid']);
              
              }else
              {
              $this->arraydata['error']="ไม่มีสิทธิ์ลบ";
              
              }
           
           
           }else
           {
                $this->arraydata['error']="ส่งข้อมูลไม่ครบ";
           
           }
           
           }
           
           function checktestpost()
           {
           if ($this->get['testpost'])
            {
            $testpost=$this->get['testpost'];
            $testpost;
            unset($this->get['testpost']);
            $this->post=$this->get;

            if (method_exists($this, $testpost))
                {
                eval("\$this->" . $testpost . "();");
                }
            else
                {
                //    echo "error";
                }
            }
            }
           
           function forgotpassword()
          {
           if($this->post['email'])
           {
           $sql=" select userid,username from tb_user where email='".$this->post['email']."'";
           $arrayuser=$this->db->db_set_recordset($sql);
           if($arrayuser[0]['userid'])
           {
           $arraydata['forgetcode']=md5(mktime().$arrayuser[0]['username'].$arrayuser[0]['email']);
           $this->db->db_set($arraydata,"tb_user"," userid=".$arrayuser[0]['userid']);
           
           
           require_once(rootpath.'/library/plugins/phpmailer/class.config.php'); // uri
           
           $mail = new ConfigMail();
           
           $mail->setTo($this->post['email'],$arrayuser[0]['username']);
           $mail->setContent('Reset Password This Link <a href="'.baseinfo.'/resetpassword?forgetcode='.$arraydata['forgetcode'].'">'.baseinfo.'/resetpassword?forgetcode='.$arraydata['forgetcode'].'</a>');
           $mail->setSubject("Reset Password From Meetme");
           $mail->sendTo();
           }else
           {
           $this->arraydata['error']=" ไม่มี user นี้ ";
           
           }
           }else
           {
           
           $this->arraydata['error']=" ส่งข้อมูลไม่ครบ ";
           
           }
           
           
           }
           
           function resetpassword()
            {
            if($this->post['forgetcode']&&$this->post['repassword'])
            {
            
            if($this->checkresetpassword($this->post['forgetcode']))
            {
            
            $arraydata['password1']=md5(baseinfo.'-'.$this->post['repassword']);
            $arraydata['forgetcode']="";
            $this->db->db_set($arraydata,"tb_user"," forgetcode='".$this->post['forgetcode']."'");
            
            }else
            {
            $this->arraydata['error']=" forgetcode ผิด ";
            
            }
            
            
            }else
            {
            $this->arraydata['error']="ส่งข้อมูลไม่ครบ";
            
            }
            
            }          
           
           function downloadBook()
           {
           if($this->post['bookid']&&$this->post['shelfid']&&$this->post['uuid'])
           {
           if(!$this->checkdownloadbook($this->post['bookid'],$this->post['shelfid'],$this->post['uuid']))
           {
           $arraydata['bookid']=$this->post['bookid'];
           $arraydata['shelfid']=$this->post['shelfid'];
           $arraydata['uuid']=$this->post['uuid'];
           $arraydata['createdate']=date("Y-m-d H:i:s");
           $this->db->db_set($arraydata,"tb_download"); 
           
           }
           $this->logdatamobile($this->post['uuid'],3,$this->info['parameter'][0],$this->post['shelfid'],$this->post['bookid']);
           }else
           {
           
           $this->arraydata['error']="ส่งข้อมูลไม่ครบ";
           }
           
           }
           
           function readNotificationToken()
           {
            if($this->post['token']&&$this->post['shelfid']&&$this->post['uuid'])
           {
           //if(!$this->checktoken($this->post['token']))
//           {
           $arraydata['badge']=0;
      //     $arraydata['shelfid']=$this->post['shelfid'];
        //   $arraydata['uuid']=$this->post['uuid'];
           $arraydata['updatedate']=date("Y-m-d H:i:s");
          // $arraydata['createdate']=date("Y-m-d H:i:s");
           $this->db->db_set($arraydata,"tb_token",' token="'.$this->post['token'].'" and shelfid='.$this->post['shelfid']);
        //  }
           
           }else
           {
           $this->arraydata['error']="ส่งข้อมูลไม่ครบ";
           
           }
           
           }
           
           function saveToken()
           {
           if($this->post['token']&&$this->post['shelfid']&&$this->post['uuid'])
           {
           if(!$this->checktoken($this->post['token'],$this->post['shelfid']))
           {
           $arraydata['token']=$this->post['token'];
           $arraydata['shelfid']=$this->post['shelfid'];
           $arraydata['uuid']=$this->post['uuid'];
           $arraydata['updatedate']=date("Y-m-d H:i:s");
           $arraydata['createdate']=date("Y-m-d H:i:s");
           $this->db->db_set($arraydata,"tb_token");
           }
          // $this->arraydata['error']=$this->post['token']."|".$this->post['shelfid']."|".$this->post['uuid'];
           
           }else
           {
           $this->arraydata['error']="ส่งข้อมูลไม่ครบ";
           
           }
           
           
           }
           
           function readDetailBook()
           {
           if($this->post['bookid']&&$this->post['shelfid']&&$this->post['uuid'])
           {
           if(!$this->checkreadbook($this->post['bookid'],$this->post['shelfid'],$this->post['uuid']))
           {
           $arraydata['bookid']=$this->post['bookid'];
           $arraydata['shelfid']=$this->post['shelfid'];
           $arraydata['uuid']=$this->post['uuid'];
           $arraydata['createdate']=date("Y-m-d H:i:s");
           $this->db->db_set($arraydata,"tb_read"); 
           
           }
           $this->logdatamobile($this->post['uuid'],3,$this->info['parameter'][0],$this->post['shelfid'],$this->post['bookid']);
           }else
           {
           
           $this->arraydata['error']="ส่งข้อมูลไม่ครบ";
           }
           
           }
           
           
           function getbook()
           {
            if($this->get['bookid']&&$this->checkuser())
            {
            
               $this->arraydata['data']=$this->getbookbybookid($this->get['bookid']);
            
            }else
            {
            
               $this->arraydata['error']="คุณไม่มีสิทธิ์ ";
            }
           
           }
           
           function getstatmybook()
           {
               $booladmin=$this->checkadmin();
               if(($this->checkuser($this->get['shelfid'])&&$this->get['shelfid'])||$booladmin)
               {
                 if($booladmin)
                 {
                 $this->arraydata['data']=$this->getstatbyuserid("",$this->get['sort'],$this->get['start'],$this->get['end'],$this->get['shelfid']); 
                 }else
                 {
                 $userdataarray=$this->getuserbyoauth($_COOKIE['oauth']);
                 $this->arraydata['data']=$this->getstatbyuserid($userdataarray['userid'],$this->get['sort'],$this->get['start'],$this->get['end']); 
                 
                 } 
                 
               
               
               
               }else
               {
               
               
               
               }
           
           
           }
           
           
           function getstatadmin()
           {

                 
                 
             if($this->get['day']||$this->get['month']||$this->get['year']||$this->get['yearstart'])
                {
                
                $day=$this->get['day'];
                $month=$this->get['month'];
                $year=$this->get['year'];
                $yearstart=$this->get['yearstart'];
                $shelfid=$this->get['shelfid'];
                $userid=$this->get['userid'];
               $sort=0;     
               if($day&&$month&&$year)
              {
               $sort=1;
              }else if($month&&$year)
              {
               $sort=2;
              
              } else if($year)
              {
               $sort=3;
              
              }else if($yearstart)
              {
               $sort=4;
              
              }
          
                if($sort)
                {
                
                $data=$this->getstatbydate($day,$month,$year,$yearstart,$shelfid,$userid); 
                
                //$this->arraydata['data']=$data;
                $arraynewdata=array();
                
                if(count($data[1]))
                {
                
               switch ($sort) { 
               case 1:
               for($i=0;$i<24;$i++)
               {
               
               $arraynewdata['labels'][]=sprintf("%02d:00",$i);
              }
                $k=0;
                foreach($data[1] as $valuedata)
                 {
                 
                 
                 
                 $arraynewdata['datasets'][$k]['name']=$valuedata['title'];
                 
                 
                 if(count($data[0]))
                 {
                 
                 for($i=0;$i<24;$i++)
                 {
                  foreach($data[0] as $valuedatastat)
                  {
                  list($date,$time)=explode(" ",$valuedatastat['createdate']);
                  list($h,$m,$s)=explode(":",$time);
                  $newi=sprintf("%02d",$i);
                  if($h==$newi)
                  {
                   
                   if($valuedatastat['bookid']==$valuedata['bookid'])
                   {
                   if($arraynewdata['datasets'][$k]['data'][$i])
                   {
                   
                   $arraynewdata['datasets'][$k]['data'][$i]=$arraynewdata['datasets'][$k]['data'][$i]+1;
                   
                   }else
                   {
                   
                   $arraynewdata['datasets'][$k]['data'][$i]=1; 
                   
                   }
                     
                   
                   }else
                   {
                   
                   if($arraynewdata['datasets'][$k]['data'][$i])
                   {
                   $arraynewdata['datasets'][$k]['data'][$i]=$arraynewdata['datasets'][$k]['data'][$i];
                   
                   }else
                   {
                   $arraynewdata['datasets'][$k]['data'][$i]=0; 
                   
                   }
                   
                   
                   
                   }
                     
                   
                       
                  
                  
                  }else
                  {
                   if($arraynewdata['datasets'][$k]['data'][$i])
                   {
                   $arraynewdata['datasets'][$k]['data'][$i]=$arraynewdata['datasets'][$k]['data'][$i];
                   
                   }else
                   {
                   $arraynewdata['datasets'][$k]['data'][$i]=0; 
                   
                   }
                  
                  }
                  
                  }
                 
                   
                 
                 }
                  $arraynewdata['datasets'][$k]['summary']=array_sum($arraynewdata['datasets'][$k]['data']); 
                 
                 }else
                 {
                 
                 $arraynewdata['datasets'][$k]['summary']=0;
                 for($i=0;$i<24;$i++)
                 {
                 
                  $arraynewdata['datasets'][$k]['data'][$i]=0;
                  
                 }
                   
                 }
                 
                $k++; 
                 
                 
                 }
              
               break;
               case 2:
               
                $d= new DateTime( sprintf("%04d-%02d-01 00:00:00", $year, $month) );
                $t=$d->format( 't' );
               
               for($i=1;$i<=$t;$i++)
               {
               
               $arraynewdata['labels'][]="".$i;
              }
              
               $k=0;
                foreach($data[1] as $valuedata)
                 {
                 
                 
                 
                 $arraynewdata['datasets'][$k]['name']=$valuedata['title'];
                 
                 
                 if(count($data[0]))
                 {
                 
                 for($i=0;$i<$t;$i++)
                 {
                  foreach($data[0] as $valuedatastat)
                  {
                  list($date,$time)=explode(" ",$valuedatastat['createdate']);
                  list($y,$m,$d)=explode("-",$date);
                  $newi=sprintf("%02d",($i+1));
                  if($d==$newi)
                  {
                   
                   if($valuedatastat['bookid']==$valuedata['bookid'])
                   {
                   if($arraynewdata['datasets'][$k]['data'][$i])
                   {
                   
                   $arraynewdata['datasets'][$k]['data'][$i]=$arraynewdata['datasets'][$k]['data'][$i]+1;
                   
                   }else
                   {
                   
                   $arraynewdata['datasets'][$k]['data'][$i]=1; 
                   
                   }
                     
                   
                   }else
                   {
                   
                   if($arraynewdata['datasets'][$k]['data'][$i])
                   {
                   $arraynewdata['datasets'][$k]['data'][$i]=$arraynewdata['datasets'][$k]['data'][$i];
                   
                   }else
                   {
                   $arraynewdata['datasets'][$k]['data'][$i]=0; 
                   
                   }
                   
                   
                   
                   }
                     
                   
                       
                  
                  
                  }else
                  {
                   if($arraynewdata['datasets'][$k]['data'][$i])
                   {
                   $arraynewdata['datasets'][$k]['data'][$i]=$arraynewdata['datasets'][$k]['data'][$i];
                   
                   }else
                   {
                   $arraynewdata['datasets'][$k]['data'][$i]=0; 
                   
                   }
                  
                  }
                  
                  }
                 
                   
                 
                 }
                  $arraynewdata['datasets'][$k]['summary']=array_sum($arraynewdata['datasets'][$k]['data']); 
                 
                 }else
                 {
                 
                 $arraynewdata['datasets'][$k]['summary']=0;
                 for($i=0;$i<$t;$i++)
                 {
                 
                  $arraynewdata['datasets'][$k]['data'][$i]=0;
                  
                 }
                   
                 }
                 
                $k++; 
                 
                 
                 }
               
               
               
               
               break;
               case 3:
              // require_once(rootpath.'/library/plugins/general_pi.php'); // uri      
               
               $t=12;
               
               for($i=1;$i<=$t;$i++)
               {
               $month_array = array(1=>"มกราคม",2=>"กุมภาพันธ์",3=>"มีนาคม",4=>"เมษายน",5=>"พฤษภาคม",6=>"มิถุนายน",7=>"กรกฏาคม",8=>"สิงหาคม",9=>"กันยายน",10=>"ตุลาคม",11=>"พฤศจิกายน",12=>"ธันวาคม");
        
               $arraynewdata['labels'][]=$month_array[$i];
              }
              
               $k=0;
                foreach($data[1] as $valuedata)
                 {
                 
                 
                 
                 $arraynewdata['datasets'][$k]['name']=$valuedata['title'];
                 
                 
                 if(count($data[0]))
                 {
                 
                 for($i=0;$i<$t;$i++)
                 {
                  foreach($data[0] as $valuedatastat)
                  {
                  list($date,$time)=explode(" ",$valuedatastat['createdate']);
                  list($y,$m,$d)=explode("-",$date);
                  $newi=sprintf("%02d",($i+1));
                  if($m==$newi)
                  {
                   
                   if($valuedatastat['bookid']==$valuedata['bookid'])
                   {
                   if($arraynewdata['datasets'][$k]['data'][$i])
                   {
                   
                   $arraynewdata['datasets'][$k]['data'][$i]=$arraynewdata['datasets'][$k]['data'][$i]+1;
                   
                   }else
                   {
                   
                   $arraynewdata['datasets'][$k]['data'][$i]=1; 
                   
                   }
                     
                   
                   }else
                   {
                   
                   if($arraynewdata['datasets'][$k]['data'][$i])
                   {
                   $arraynewdata['datasets'][$k]['data'][$i]=$arraynewdata['datasets'][$k]['data'][$i];
                   
                   }else
                   {
                   $arraynewdata['datasets'][$k]['data'][$i]=0; 
                   
                   }
                   
                   
                   
                   }
                     
                   
                       
                  
                  
                  }else
                  {
                   if($arraynewdata['datasets'][$k]['data'][$i])
                   {
                   $arraynewdata['datasets'][$k]['data'][$i]=$arraynewdata['datasets'][$k]['data'][$i];
                   
                   }else
                   {
                   $arraynewdata['datasets'][$k]['data'][$i]=0; 
                   
                   }
                  
                  }
                  
                  }
                 
                   
                 
                 }
                  $arraynewdata['datasets'][$k]['summary']=array_sum($arraynewdata['datasets'][$k]['data']); 
                 
                 }else
                 {
                 
                 $arraynewdata['datasets'][$k]['summary']=0;
                 for($i=0;$i<$t;$i++)
                 {
                 
                  $arraynewdata['datasets'][$k]['data'][$i]=0;
                  
                 }
                   
                 }
                 
                $k++; 
                 
                 
                 }
               
               
               
               
               break; 
                case 4:
            //   require_once(rootpath.'/library/plugins/general_pi.php'); // uri      
               
               $t=date("Y")-$yearstart;
               //echo $t;
               if($t==0)
               {
                $arraynewdata['labels'][]=date("Y");
               }else
               {
               
               
               for($i=0;$i<=$t;$i++)
               {
           //    $month_array = array(1=>"มกราคม",2=>"กุมภาพันธ์",3=>"มีนาคม",4=>"เมษายน",5=>"พฤษภาคม",6=>"มิถุนายน",7=>"กรกฏาคม",8=>"สิงหาคม",9=>"กันยายน",10=>"ตุลาคม",11=>"พฤศจิกายน",12=>"ธันวาคม");
              // echo   $i."<br>";
               $arraynewdata['labels'][]="".($yearstart+$i);
              }
              
              //echo "<pre>";
//              print_r($arraynewdata);
//              echo "</pre>;";
               }
              
               $k=0;
                foreach($data[1] as $valuedata)
                 {
                 
                 
                 
                 $arraynewdata['datasets'][$k]['name']=$valuedata['title'];
                 
                 
                 if(count($data[0]))
                 {
                 
                 for($i=0;$i<=$t;$i++)
                 {
                  foreach($data[0] as $valuedatastat)
                  {
                  list($date,$time)=explode(" ",$valuedatastat['createdate']);
                  list($y,$m,$d)=explode("-",$date);
                  $newi=sprintf("%04d",($yearstart+$i));
                  if($y==$newi)
                  {
                   
                   if($valuedatastat['bookid']==$valuedata['bookid'])
                   {
                   if($arraynewdata['datasets'][$k]['data'][$i])
                   {
                   
                   $arraynewdata['datasets'][$k]['data'][$i]=$arraynewdata['datasets'][$k]['data'][$i]+1;
                   
                   }else
                   {
                   
                   $arraynewdata['datasets'][$k]['data'][$i]=1; 
                   
                   }
                     
                   
                   }else
                   {
                   
                   if($arraynewdata['datasets'][$k]['data'][$i])
                   {
                   $arraynewdata['datasets'][$k]['data'][$i]=$arraynewdata['datasets'][$k]['data'][$i];
                   
                   }else
                   {
                   $arraynewdata['datasets'][$k]['data'][$i]=0; 
                   
                   }
                   
                   
                   
                   }
                     
                   
                       
                  
                  
                  }else
                  {
                   if($arraynewdata['datasets'][$k]['data'][$i])
                   {
                   $arraynewdata['datasets'][$k]['data'][$i]=$arraynewdata['datasets'][$k]['data'][$i];
                   
                   }else
                   {
                   $arraynewdata['datasets'][$k]['data'][$i]=0; 
                   
                   }
                  
                  }
                  
                  }
                 
                   
                 
                 }
                  $arraynewdata['datasets'][$k]['summary']=array_sum($arraynewdata['datasets'][$k]['data']); 
                 
                 }else
                 {
                 
                 $arraynewdata['datasets'][$k]['summary']=0;
                   if($t==0)
               {
                $arraynewdata['datasets'][$k]['data'][$i]=0; 
               }else
               {
                 for($i=0;$i<$t;$i++)
                 {
                 
                  $arraynewdata['datasets'][$k]['data'][$i]=0;
                  
                 }
               }
                   
                 }
                 
                $k++; 
                 
                 
                 }
               
               
               
               
               break;  
                
                
                }
                
                
                
                
                
              $this->arraydata['data']=$arraynewdata;  
                
                }else
                {
                
                $this->arraydata['error']="ไม่มีข้อมูลของ shelf นี้";
                
                }
                
                
                
                
                }else
                {
               $this->arraydata['error']="ข้อมูลไม่ครบ"; 
                
                }
                
                
                
                }else
                {
               $this->arraydata['error']="ข้อมูลไม่ครบ"; 
                
                }
               
               
               
          
           
           
           }
           
           function exportshelf()
           {
            if($this->checkadmin()&&$this->post['shelfid'])
            {
            $shelfid=$this->post['shelfid'];
            require_once(rootpath.'/library/plugins/general_pi.php'); // uri
            //echo $shelfid;
            @unlink(dbpath.'/'.$shelfid.'-'.date("Y-m-d").'.sql');
            $this->getdatabasebyshelfid($shelfid);
            //exit;
            
            
            if(is_dir(imgpath.'/'.$shelfid))
            {
            getFilesFromDirMuti(imgpath.'/'.$shelfid,$arraynow);
            
            }
            
            
            
            
           // echo "<pre>";
//            print_r($arraynow);
//            echo "</pre>;";
//            exit;
            if(count($arraynow))
            {
            
            $zip = new ZipArchive;
            $zip_archive = zippath.'/'.$shelfid.'-'.date("Y-m-d").'.zip';
            @unlink($zip_archive);
            $res = $zip->open($zip_archive, ZipArchive::CREATE);
            
            foreach($arraynow as $valuenow)
            {
            
            $zip->addFile($valuenow,str_replace(rootpath.'/assets/','',$valuenow));
            
            }
            
            if(is_dir(filepath.'/'.$shelfid))
            {
            getFilesFromDirMuti(filepath.'/'.$shelfid,$arraynow2);
            
            }
            
            if(count($arraynow2))
            {
            
            foreach($arraynow2 as $valuenow2)
            {
            
            $zip->addFile($valuenow2,str_replace(rootpath.'/assets/','',$valuenow2));
            
            }
            
            
            }
            
            if(is_file(dbpath.'/'.$shelfid.'-'.date("Y-m-d").'.sql'))
            {
            $zip->addFile(dbpath.'/'.$shelfid.'-'.date("Y-m-d").'.sql',db_name.'.sql');
            
            }
            
            
            
            
            $zip->close();
            chmod(zippath.'/'.$shelfid.'-'.date("Y-m-d").'.zip',0777);
            $this->arraydata['zip']=$shelfid.'-'.date("Y-m-d").'.zip';
            }
            
            }
           
           
           }
           
           function removeshelf()
           {
              if($this->checkadmin())
              {
              if($this->post['shelfid'])
              {
              $shelfid=$this->post['shelfid'];
              require_once(rootpath.'/library/plugins/general_pi.php'); // uri
              
              $sql="delete from tb_banner where shelfid=".$shelfid; 
              $this->db->db_delete($sql);
              
              $sql="delete from tb_banner_library where shelfid=".$shelfid; 
              $this->db->db_delete($sql);
              
              $sql="delete from tb_shelf where shelfid=".$shelfid; 
              $this->db->db_delete($sql);
              
              $sql="delete from tb_user where shelfid=".$shelfid; 
              $this->db->db_delete($sql);
              
              $sql="delete from tb_tag where shelfid=".$shelfid; 
              $this->db->db_delete($sql);
              
              $sql="delete from tb_cat where shelfid=".$shelfid; 
              $this->db->db_delete($sql);
              
              deleteAll(imgpath.'/'.$shelfid); 
              @deleteAll(filepath.'/'.$shelfid);
              
              
              $sql="select bookid from tb_book where shelfid=".$shelfid;
              $arraybook=$this->db->db_set_recordset($sql);
              
              if(count($arraybook))
              {
              foreach($arraybook as $valuebook)
              {
              $arraydata['bookid']=$valuebook['bookid'];
              $arraydata['shelfid']=$shelfid;
              $arraydata['createdate']=date("Y-m-d H:i:s");
              $this->db->db_set($arraydata,"tb_del_book");
              
              
              $sql="delete from tb_book where shelfid=".$shelfid.' and bookid='.$valuebook['bookid']; 
              $this->db->db_delete($sql);
              
              $sql="delete from tb_tag_relate where   bookid=".$valuebook['bookid']; 
              $this->db->db_delete($sql);
              
              $sql="delete from tb_cat_relate where   bookid=".$valuebook['bookid']; 
              $this->db->db_delete($sql);
              
              $sql="delete from tb_file where   bookid=".$valuebook['bookid']; 
              $this->db->db_delete($sql);
              
              
              
              }
              
              }
              
              }else
              {
              $this->arraydata['error']="ส่งข้อมูลไม่ครบ";
              
              }
              
              
              }else
              {
              $this->arraydata['error']="คุณไม่ช่าย Admin";
              
              }
            
           
           }
           
           //http://dpportal.80root.com/ajax/redirectpage?shelfid=4&nojson=1
           function redirectpage()
           {
           if($this->get['shelfid'])
           {
             $shelfid=$this->get['shelfid'];
             $sql="SELECT
                tb_shelf.itune,tb_shelf.schema
                FROM
                tb_shelf
               
                where  tb_shelf.shelfid=" . $shelfid . "
                ";
            $arraydata=$this->db->db_set_recordset($sql);
           // $arraydata=$this->db->db_get_recordset();
//            $this->db->destory();
           
           if($arraydata[0]['itune'])
           {
           $itune=$arraydata[0]['itune'];
           }else
           {
           $itune="https://itunes.apple.com/us/app/infostant/id548216775";
           }
           
           if($arraydata[0]['schema'])
           {
           $schema=$arraydata[0]['schema'];
           }else
           {
           $schema="infostant";
           }

         //   header("Location:" . $arraydata[0]['itune']);
            //$this->db->closedb();
            
        if(count($this->get))
        {
        $strget=http_build_query($this->get);
        }

        
     
        $code  = '<!DOCTYPE html>'."\n";
        $code .= '<html>'."\n";
        $code .= '<head>'."\n";
        $code .= '<title></title>'."\n";
        $code .= '<script>var downloadLink="'.$itune.'";var appScheme="'.$schema.'://q?'.$strget.'";if(navigator.userAgent.match(/(iPad|iPhone|iPod)/g)){setTimeout(function(){window.location=downloadLink},25);window.location=appScheme}else{document.location=downloadLink};</script>'."\n";
        $code .= '</head>'."\n";
        $code .= '<body>'."\n";
        $code .= '</body>'."\n";
        $code .= '</html>';


        echo $code;
        exit;
        } 
           
           }
           function phpinicheck()
           {
           
           phpinfo();
           }
           function saveshelf()
            {
              if($this->checkadmin())
              {
              
               
              
              
               $userdataarray=$this->getuserbyoauth($_COOKIE['oauth']);
               //exit;

               $shelfid=$this->post['shelfid'];
               $arraydata['title']=$this->post['title'];
               $arraydata['descritpion']=$this->post['descritpion'];
               $arraydata['photo']=$this->post['photo'];
               //$arraydata['passpem']=$this->post['passpem'];
               //$arraydata['pemfile']=$this->post['pemfile'];
              // $arraydata['itune']=$this->post['itune'];
          //     $arraydata['schema']=$this->post['schema'];
               
               
               $arraydata['updatedate']=date("Y-m-d H:i:s");
               $arraydata['updateby']=$userdataarray['userid']; 
               
               if($shelfid)
               {
               $sql="select shelfid from tb_shelf where photo='".$this->post['photo']."' and shelfid=$shelfid ";
               $arrayshelf=$this->db->db_set_recordset($sql);
               if(!$arrayshelf[0]['shelfid'])
               {
                copy(cacheimgpath.'/'.$this->post['photo'],imgpath.'/'.$shelfid.'/'.$this->post['photo']);
                chmod(imgpath.'/'.$shelfid.'/'.$this->post['photo'],0777);
               
               }
               
               
               //$sql="select shelfid from tb_shelf where pemfile='".$this->post['pemfile']."' and shelfid=$shelfid ";
//               $arrayshelf=$this->db->db_set_recordset($sql);
               if($this->post['pemfile'])
               {
               if(!is_file(filepath.'/'.$shelfid.'/'.$this->post['pemfile']))
               {
                copy(cacheimgpath.'/'.$this->post['pemfile'],filepath.'/'.$shelfid.'/'.$this->post['pemfile']);
                chmod(filepath.'/'.$shelfid.'/'.$this->post['pemfile'],0777);
               
               }
               }

               
               $this->db->db_set($arraydata,"tb_shelf",' shelfid='.$shelfid); 
               $version=$this->getversion($shelfid);
               if($version)
               {
               
             //  $arrayversion['shelfid']=$shelfid;
               $arrayversion['version']=$version+1;
               $this->db->db_set($arrayversion,"tb_version",' shelfid='.$shelfid);
               
               }else
               {
               $arrayversion['shelfid']=$shelfid;
               $arrayversion['version']=1;
               $this->db->db_set($arrayversion,"tb_version");
               
               }
               
               }else
               {
               $set=1;
               $arraydata['createby']=$userdataarray['userid'];
               $arraydata['createdate']=date("Y-m-d H:i:s");
               $this->db->db_set($arraydata,"tb_shelf");
               $shelfid=$this->db->db_get_last_number();
               $arrayversion['shelfid']=$shelfid;
               $arrayversion['version']=1;
               $this->db->db_set($arrayversion,"tb_version");
               
               }
               
               if($set)
               {
               
               if(!is_dir(imgpath.'/'.$shelfid))
               {
               mkdir(imgpath.'/'.$shelfid,0777);
               chmod(imgpath.'/'.$shelfid,0777);
               }
               
               if(!is_dir(filepath.'/'.$shelfid))
               {
               mkdir(filepath.'/'.$shelfid,0777);
               chmod(filepath.'/'.$shelfid,0777);
               }
               
               
               if($this->post['pemfile'])
               {
               copy(cacheimgpath.'/'.$this->post['pemfile'],filepath.'/'.$shelfid.'/'.$this->post['pemfile']);
               chmod(filepath.'/'.$shelfid.'/'.$this->post['pemfile'],0777);
               
               }
               
               copy(cacheimgpath.'/'.$this->post['photo'],imgpath.'/'.$shelfid.'/'.$this->post['photo']);
               chmod(imgpath.'/'.$shelfid.'/'.$this->post['photo'],0777);
               
               
               
               
               
               if(!is_dir(imgpath.'/'.$shelfid.'/banner'))
               {
               mkdir(imgpath.'/'.$shelfid.'/banner',0777);
               chmod(imgpath.'/'.$shelfid.'/banner',0777);
               }
               }
           //    
//               $sql="delete from tb_banner where shelfid=".$shelfid; 
//               $this->db->db_delete($sql);
//               
//               for($i=1;$i<=4;$i++)
//               {
//               if($this->post['banner'.$i]&&$this->post['link'.$i])
//               {
//               $arraybanner['photo']=$this->post['banner'.$i];
//               $arraybanner['link']=$this->post['link'.$i]; 
//               $arraybanner['shelfid']=$shelfid;
//               $arraybanner['sort']=$i;
//               $arraybanner['createdate']=date("Y-m-d H:i:s");
//               $this->db->db_set($arraybanner,"tb_banner");
//               
//               if(!is_file(imgpath.'/'.$shelfid.'/banner/'.$arraybanner['photo']))
//               {
//               copy(cacheimgpath.'/'.$arraybanner['photo'],imgpath.'/'.$shelfid.'/banner/'.$arraybanner['photo']);
//               chmod(imgpath.'/'.$shelfid.'/banner/'.$arraybanner['photo'],0777);
//               
//               }
//               
//               }
//               }
//               
//               
//               $sql="delete from tb_banner_library where shelfid=".$shelfid; 
//               $this->db->db_delete($sql);
//               
//               $i=5;
//               if($this->post['banner'.$i]&&$this->post['link'.$i])
//               {
//               $arraybanner['photo']=$this->post['banner'.$i];
//               $arraybanner['link']=$this->post['link'.$i]; 
//               $arraybanner['shelfid']=$shelfid;
//               $arraybanner['sort']=$i;
//               $arraybanner['createdate']=date("Y-m-d H:i:s");
//               $this->db->db_set($arraybanner,"tb_banner_library");
//               
//               if(!is_file(imgpath.'/'.$shelfid.'/banner/'.$arraybanner['photo']))
//               {
//               copy(cacheimgpath.'/'.$arraybanner['photo'],imgpath.'/'.$shelfid.'/banner/'.$arraybanner['photo']);
//               chmod(imgpath.'/'.$shelfid.'/banner/'.$arraybanner['photo'],0777);
//               
//               }
//               
//               }
//               
               if(count($this->post['username']))
               {
               $k=0;
               $arrayuser=array();
               foreach($this->post['username'] as $valueusername)
               {
               
               
               
               $arrayuser=array();
               $check=0;
               if($this->post['userid'][$k])
               {
               
               if(!$this->checkuserbyusername($valueusername))
               {
               $check=1;
              $arrayuser['username']=$valueusername;
               }
               
              if(!$this->checkuserbyemail($this->post['email'][$k]))
               {
               $check=1;
             // $arrayuser['email']=$this->post['email'][$k];
               }
               
               if(isset($this->post['roleid'][$k]))
               {
                 $check=1;
                $arrayuser['roleid']=$this->post['roleid'][$k];
               }
               
              
               
               $arrayuser['firstname']=$this->post['firstname'][$k]; 
               
               if($check==1)
               {
               $arrayuser['updatedate']=date("Y-m-d H:i:s"); 
               

               $this->db->db_set($arrayuser,"tb_user",' userid='.$this->post['userid'][$k]);
               }
               
               }
               else
               {
               
                if($this->checkuserbyusername($valueusername)||$this->checkuserbyemail($this->post['email'][$k])||$valueusername==""||$this->post['email'][$k]=="")
               {
               $check=1;
               
               }
               
              
               
             
               if($check==0)
               {
               $arrayuser['username']=$valueusername;
               $arrayuser['email']=$this->post['email'][$k];
               if($this->post['roleid'][$k])
               {
                $arrayuser['roleid']=1;
               }else
               {
                $arrayuser['roleid']=0;
               }
               
               if($this->post['photoavatar'][$k])
               {
               
               $arrayuser['photo']=$this->post['photoavatar'][$k];
                
               
               
               }
               
               $arrayuser['firstname']=$this->post['firstname'][$k]; 
               $arrayuser['updatedate']=date("Y-m-d H:i:s");
               $arrayuser['createdate']=date("Y-m-d H:i:s");
               $arrayuser['shelfid']=$shelfid;
               $arrayuser['password1']=md5(baseinfo.'-'.$this->post['password'][$k]);
               $arrayuser['oauth']=md5(baseinfo.'-'.$arrayuser['updatedate'].'-'.rand(0,1000000)); 
               $this->db->db_set($arrayuser,"tb_user");
               
               $userid=$this->db->db_get_last_number(); 
               
               
                if(!is_dir(imgpath.'/user/'.$userid))
                   {
                   mkdir(imgpath.'/user/'.$userid,0777);
                   chmod(imgpath.'/user/'.$userid,0777);
                   }
               
               if($this->post['photoavatar'][$k])
               {
               @copy(baseinfo.'/assets/cache/'.$this->post['photoavatar'][$k],imgpath.'/user/'.$userid.'/'.$this->post['photoavatar'][$k]);
               @chmod(imgpath.'/user/'.$userid.'/'.$this->post['photoavatar'][$k],0777);
               
               }
               
               
               
               }
               
               }
               
               
               $k++;
               }
               
               if(count($this->post['deleteuserid']))
               {
               foreach($this->post['deleteuserid'] as $valueuserid)
               {
               if($valueuserid)
               {
               $sql="delete from tb_user where userid=".$valueuserid; 
               $this->db->db_delete($sql);
               }
               
               }
               
               
               }
               
               }
               
               if(count($this->post['category']))
               {
               $k=0;
               $arraycategory=array();
               foreach($this->post['category'] as $valuename)
               {
               
               
               
               
               $check=0;
               if($this->post['catid'][$k])
               {
               if(!$this->checkcatbycatname($valuename,$shelfid))
               {
               $check=1;
               $arraycategory['name']=$valuename;
               }
               
             
               
               if($check==1)
               {
               $arraycategory['updateby']=$userdataarray['userid']; 
               $arraycategory['updatedate']=date("Y-m-d H:i:s");
               $this->db->db_set($arraycategory,"tb_cat",' catid='.$this->post['catid'][$k]);
               }
               
               }
               else
               {
               
                if($this->checkcatbycatname($valuename,$shelfid))
               {
               $check=1;
               
               }
               
             
               if($check==0)
               {
               $arraycategory['name']=$valuename;
               $arraycategory['shelfid']=$shelfid;
               $arraycategory['createby']=$userdataarray['userid'];
               $arraycategory['updateby']=$userdataarray['userid'];
               $arraycategory['updatedate']=date("Y-m-d H:i:s");
               $arraycategory['createdate']=date("Y-m-d H:i:s");
             
               $this->db->db_set($arraycategory,"tb_cat");
               }
               
               }
               
               
               $k++;
               }
               
               if(count($this->post['deletecatid']))
               {
               foreach($this->post['deletecatid'] as $valuecatid)
               {
               if($valuecatid&&$shelfid)
               {
               $sql="delete from tb_cat where catid=".$valuecatid.' and shelfid='.$shelfid; 
               $this->db->db_delete($sql);
               
               $sql="delete from tb_cat_relate where catid=".$valuecatid; 
               $this->db->db_delete($sql);
               
               }
               
               }
               
               
               }
               
               }
                           
               if(count($this->post['tag']))
               {
               $k=0;
               $arraytag=array();
               foreach($this->post['tag'] as $valuename)
               {
               
               
               
               
               $check=0;
               if($this->post['tagid'][$k])
               {
               if(!$this->checktagbytagname($valuename,$shelfid))
               {
               $check=1;
               $arraytag['name']=$valuename;
               }
               
             
               
               if($check==1)
               {
               $arraytag['updateby']=$userdataarray['userid']; 
               $arraytag['updatedate']=date("Y-m-d H:i:s");
               $this->db->db_set($arraytag,"tb_tag",' tb_tag.tagid='.$this->post['tagid'][$k]);
               }
               
               }
               else
               {
               
                if($this->checktagbytagname($valuename,$shelfid))
               {
               $check=1;
               
               }
               
             
               if($check==0)
               {
               $arraytag['name']=$valuename;
               $arraytag['shelfid']=$shelfid;
               $arraytag['createby']=$userdataarray['userid'];
               $arraytag['updateby']=$userdataarray['userid'];
               $arraytag['updatedate']=date("Y-m-d H:i:s");
               $arraytag['createdate']=date("Y-m-d H:i:s");
             
               $this->db->db_set($arraytag,"tb_tag");
               }
               
               }
               
               
               $k++;
               }
               
               if(count($this->post['deletetagid']))
               {
               foreach($this->post['deletetagid'] as $valuetagid)
               {
               if($valuetagid)
               {
               $sql="delete from tb_tag where tagid=".$valuetagid; 
               $this->db->db_delete($sql);
               
               $sql="delete from tb_tag_relate where tagid=".$valuetagid; 
               $this->db->db_delete($sql);
               
               }
               
               }
               
               
               }
               
               }
              
              
              }else
              {
              $this->arraydata['error']="คุณไม่ใช่ Admin";
              }
              
           }
           
           
           
           function uploadfile()
           {
             if($_FILES["Filedata"]["tmp_name"]&&$this->checkuser())
              {
              
              $newfie=mktime().'-'.rand(0,100000); 
              
               if($this->post['folder'])
                   {
                    $setfolder=$this->post['folder'];
                    
                     if(!is_dir(imgpath.'/'.$setfolder))
                     {
                      mkdir(imgpath.'/'.$setfolder,0777);
                      chmod(imgpath.'/'.$setfolder,0777);
                      }
                      
                   $folder=imgpath.'/'.$setfolder;
                    
                   }else
                   {
                   $folder=cacheimgpath;
                   
                   }
               $typefile=".jpg";
              if($this->post['typefile'])
              {
              $typefile=".".$this->post['typefile'];
              }
              if(move_uploaded_file($_FILES["Filedata"]["tmp_name"], $folder.'/'.$newfie.$typefile)) 
                   {
                   $this->arraydata['filelink']=str_replace(rootpath,baseinfo,$folder).'/'.$newfie.$typefile;
                   $this->arraydata['filename']=$newfie.$typefile; 
                   }
                  // echo array2json($arraydata);  
              }
              else
              {
                $this->arraydata['error']="คุณ Hack มานะ ";
              
              } 
           
           }
           
           function __destruct() 
           {
           
              if(count($this->db->db_get_error()))
              {
                $sqlerror=join("|",$this->db->db_get_error());
                $this->arraydata['error']=$sqlerror;
              }else if($this->arraydata['error']=="")
              {
                $this->arraydata['error']=0;
              }

              echo   json_encode($this->arraydata);
              $this->db->closedb(); 
            }
            
            function testpushnotification()
            {
            $pem=filepath.'/3/1366860531-67743.pem';
            $arraydata[0]['token']="d0bd2ce5340d55436ad3a4e7b5a9317f4afc7b2b1c793ab294c4e318e741559d";
            $arraydata[0]['alert']="Hello world";
            $arraydata[0]['badge']=1;
            $arraydata[0]['type']=1;
            $pass="123456";
            
            $this->pushnotification($arraydata,$pem,$pass);
            
            }
            function savemeetmeurl()
            {
            if($this->get['url'])
            {
            
            $this->arraydata['url']=$this->get['url'];
            
            }else
            {
            $this->arraydata['url']='meetme.dmconnex.com';
            
            
            }
            
            
            }
            
            function cleanuserdatauser()
            {
            $sql="select * from tb_user where username='suttivat'";
               $arrayuser2=$this->db->db_set_recordset($sql);
               
               
               if(count($arrayuser2))
               {
               $k=0;
               
               foreach($arrayuser2 as $valueusername)
               {
               
               
               $arrayuser=array();
               $arrayuser['username']=$valueusername['email'];
               
               $this->db->db_set($arrayuser,"tb_user",' userid='.$valueusername['userid']);
               
               }
               
               
               }
            
            }
            
            function cleanuserdata()
            {
            
               $sql="select * from tb_user_bk";
               $arrayuser2=$this->db->db_set_recordset($sql);  
            

               if(count($arrayuser2))
               {
               $k=0;
               
               foreach($arrayuser2 as $valueusername)
               {
               
               
               

               
               $check=0;


               
                if($this->checkuserbyusername($valueusername['username'])||$this->checkuserbyemail($valueusername['username']))
               {
               $check=1;
               
               }
               
               
               if($check==0)
               {
               $arrayuser=array();
               $photofile=mktime().'-'.rand(0,100000).'.jpg';
               $arrayuser['username']=$valueusername['username'];
               $arrayuser['email']=$valueusername['username'];
             //  $arrayuser['photo']=$photofile;
               $arrayuser['firstname']=$valueusername['name'];
               
               
               if($this->post['roleid'][$k])
               {
                $arrayuser['roleid']=1;
               }else
               {
                $arrayuser['roleid']=0;
               }
               
               
               
               
               
               $arrayuser['updatedate']=date("Y-m-d H:i:s");
               //$arrayuser['createdate']=date("Y-m-d H:i:s");
              // $arrayuser['shelfid']=1;
               //$arrayuser['password1']=md5(baseinfo.'-'.$valueusername['password1']);
            //   $arrayuser['oauth']=md5(baseinfo.'-'.$arrayuser['updatedate'].'-'.rand(0,1000000)); 
               $this->db->db_set($arrayuser,"tb_user");
               $userid=$this->db->db_get_last_number();
               
               
               if(!is_dir(imgpath.'/user/'.$userid))
                   {
                   mkdir(imgpath.'/user/'.$userid,0777);
                   chmod(imgpath.'/user/'.$userid,0777);
                   }
               
               
               @copy(baseinfo.'/images/user_c/'.$valueusername['avatar'],imgpath.'/user/'.$userid.'/'.$photofile);
               @chmod(imgpath.'/user/'.$userid.'/'.$photofile,0777);
               
               
               
               }
               
             
               
               
               $k++;
               }
               
               if(count($this->post['deleteuserid']))
               {
               foreach($this->post['deleteuserid'] as $valueuserid)
               {
               if($valueuserid)
               {
               $sql="delete from tb_user where userid=".$valueuserid; 
               $this->db->db_delete($sql);
               }
               
               }
               
               
               }
               
               }
            
            
            
            }
           
           
             function cleanuserdatayyy()
            {
            
               $sql="select * from tb_user_bk";
               $arrayuser2=$this->db->db_set_recordset($sql);  
            

               if(count($arrayuser2))
               {
               $k=0;
               
               foreach($arrayuser2 as $valueusername)
               {
               
               
               

               
               $check=0;


               
      
               
               

               $arrayuser=array();
               $photofile=mktime().'-'.rand(0,100000).'.jpg';
              // $arrayuser['username']=$valueusername['username'];
              // $arrayuser['email']=$valueusername['username'];
             //  $arrayuser['photo']=$photofile;
               $arrayuser['firstname']=$valueusername['name'];
               
               
           
               
               
               
               
               
               $arrayuser['updatedate']=date("Y-m-d H:i:s");
               //$arrayuser['createdate']=date("Y-m-d H:i:s");
              // $arrayuser['shelfid']=1;
               //$arrayuser['password1']=md5(baseinfo.'-'.$valueusername['password1']);
            //   $arrayuser['oauth']=md5(baseinfo.'-'.$arrayuser['updatedate'].'-'.rand(0,1000000)); 
               $this->db->db_set($arrayuser,"tb_user",' email="'.$valueusername['username'].'"');
               $userid=$this->db->db_get_last_number();
               
               
           //    if(!is_dir(imgpath.'/user/'.$userid))
//                   {
//                   mkdir(imgpath.'/user/'.$userid,0777);
//                   chmod(imgpath.'/user/'.$userid,0777);
//                   }
               
               
      //         @copy('http://meetme.thaihealth.or.th/images/user_c/'.$valueusername['avatar'],imgpath.'/user/'.$userid.'/'.$photofile);
//               @chmod(imgpath.'/user/'.$userid.'/'.$photofile,0777);
               
               
               
               
               
             
               
               
               $k++;
               }
               
               if(count($this->post['deleteuserid']))
               {
               foreach($this->post['deleteuserid'] as $valueuserid)
               {
               if($valueuserid)
               {
               $sql="delete from tb_user where userid=".$valueuserid; 
               $this->db->db_delete($sql);
               }
               
               }
               
               
               }
               
               }
            
            
            
            }
           
           
           
      }
?>
